//! pGina winLDAP plugin.
/*!

Some code has been taken from ldapAuth_plus (http://www.pgina.org/index.php/Plugins:LDAP_Auth). 
Please observe their licenses as well if you use develop based upon this code. 


LTIA - Applied Information Technology Lab
UNESP - Univ Estadual Paulista, Bauru

*/

#pragma once

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "pgina_headers.h"
#include "messages_def.h"
#include "des.h"
#include "../mkntpwd/mkntpwd.h"
#include <wchar.h>
#include <WCHAR.h>
#include <Winldap.h>
#include <Winreg.h>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <Winber.h>
#include <Lm.h>

#ifdef _DEBUG
#include <atltime.h>
#endif

// Global Values
#define MISCONFIGURED 0
#define MULTIMAP	1
#define SEARCH		2
#define STRMAP		3
#define MacOSX		1
#define NDS			2
#define Samba		3
// Well-known users and groups...
#define DOMAIN_USER_RID_ADMIN          (0x000001F4L)
#define DOMAIN_USER_RID_GUEST          (0x000001F5L)
#define DOMAIN_GROUP_RID_ADMINS        (0x00000200L)
#define DOMAIN_GROUP_RID_USERS         (0x00000201L)
#define DOMAIN_GROUP_RID_GUESTS        (0x00000202L)
// well-known aliases ...
#define DOMAIN_ALIAS_RID_ADMINS        (0x00000220L)
#define DOMAIN_ALIAS_RID_USERS         (0x00000221L)
#define DOMAIN_ALIAS_RID_GUESTS        (0x00000222L)
#define DOMAIN_ALIAS_RID_POWER_USERS   (0x00000223L)
#define DOMAIN_ALIAS_RID_ACCOUNT_OPS   (0x00000224L)
#define DOMAIN_ALIAS_RID_SYSTEM_OPS    (0x00000225L)
#define DOMAIN_ALIAS_RID_PRINT_OPS     (0x00000226L)
#define DOMAIN_ALIAS_RID_BACKUP_OPS    (0x00000227L)
#define DOMAIN_ALIAS_RID_REPLICATOR    (0x00000228L)

#define LDAPAUTHVERSION TEXT("winLDAP Version 1.0 by LTIA from UNESP/Brazil")
// Prototypes
long regLookup(WCHAR *,WCHAR *);
PWCHAR * ldapSearchAttr(LPTSTR, WCHAR *, LDAP *, WCHAR *, bool ldpOverride);
void plugError(LPCWSTR *, LPVOID, DWORD);
bool ldapDoBindTest(LPTSTR, LPTSTR, LDAP *);
bool ldapTestMap(LPTSTR, LPTSTR, WCHAR *, WCHAR *, LDAP *, WCHAR *);
//bool ldapMapGo(LPTSTR, LPTSTR, LPTSTR, LPTSTR, LPTSTR, LDAP *, WCHAR *);
WCHAR * getContext(WCHAR *);
bool ldapSearchForDNGeneric(WCHAR *, WCHAR *, WCHAR *, LDAP *, WCHAR *, WCHAR *, WCHAR *, DWORD); //added by Marc
bool ldapSearchForDN(LPTSTR, WCHAR *, WCHAR *, LDAP *); 
bool nldapContextLookup(LPTSTR, WCHAR *); //added by Marc
bool ldapTestMultiMap(LPTSTR, LPTSTR, WCHAR *, LDAP *, WCHAR *);
bool regSet(WCHAR *, WCHAR *);
bool ldapTestSearch(LPTSTR, LPTSTR, WCHAR *, LDAP *, WCHAR *);
DWORD determineLdap(void);
bool ldapGroupCheck(WCHAR *, WCHAR *, LDAP *,WCHAR *);
bool okToLogin(WCHAR *, LDAP *, WCHAR *);
bool createAsAdmin(WCHAR *, LDAP *, WCHAR *);
PWCHAR * ldapSearchAttr(LPTSTR, WCHAR *, LDAP *, WCHAR *);
void populateUserInfo(pGinaInfo *, LDAP *, WCHAR *);
void setNDSReg(WCHAR *, WCHAR *);
void confNDSClient(WCHAR *, WCHAR *);
BOOL whoIsGroup(WCHAR *, DWORD);
TCHAR * regReadString(LPCTSTR location);// Added by nate
//TCHAR * regReadStringAdmin(LPCTSTR location);
LPTSTR ReadProtectedStringValueFromRegistry(LPCTSTR location);
TCHAR * regReadStringDefault(LPCTSTR, TCHAR *); //Added by Marc
BOOL regWriteString(LPTSTR value,LPCTSTR location); // added by nate
//BOOL regWriteStringAdmin(LPTSTR value,LPCTSTR location);
LONG WriteProtectedStringValueToRegistry(LPCTSTR value, LPCTSTR location);

BOOL regWriteDword(DWORD,LPCTSTR); // added by nate
DWORD regReadDword(LPCTSTR); // added by nate
void regDelValue(LPCTSTR); //added by nate
BOOL regReadBool(LPCWSTR location); //stolen from nate (one of these days hopefully it will be a library function)
//! doLdapSearch -- a standard search function for ldapauth
/*! doLdapSearch is an attempt to build a standard search function into ldapauth to
handle assorted searching needs. It will accept what is saught.
*/
void doLdapSearch(WCHAR ldapFilter[256]);

WCHAR * mapMacOSHome(pGinaInfo *, LDAP *, WCHAR *);
WCHAR * mapNDSHome(pGinaInfo *, LDAP *, WCHAR *);
void mapNDSHomeLookup(pGinaInfo *, LDAP *, WCHAR *, WCHAR *, WCHAR *);
WCHAR * mapSambaHome(pGinaInfo *, LDAP *, WCHAR *);
WCHAR * setSambaHomeDrive(pGinaInfo *, LDAP *, WCHAR *);

void mkWinPass(WCHAR * strPassIn, char * ntPassConv, char * lmPassConv);
LDAP * doLdapInit(WCHAR * ldapServer, int ldapPort, bool useSSL);

#ifdef _DEBUG
void debugOut(WCHAR *,...);
#endif 

// function pointers for mb stuff
typedef char * (*lpfnDllCrypt) (char *,char *);
typedef void (*lpfnDllHash) (char *, char *, char *);


//! Cldapauth_plusApp
/* See ldapauth_plus.cpp for the implementation of this class
*/
class Cldapauth_plusApp : public CWinApp
{
public:
	Cldapauth_plusApp();

// Overrides
public:
	virtual BOOL InitInstance();

	DECLARE_MESSAGE_MAP();
};



/* Renato Schmidt <renato.schmidt@ltia.fc.unesp.br> 2009/12/14 
   WindowsGroupMap
*/
bool windowsGroupMap(pGinaInfo *, LDAP *, WCHAR *);
